import * as constants from './constants';
import { fromJS } from 'immutable';
import { getAllConfigsAxios, getAllDatabaseAxios, testConAxios, saveConAxios, deleteConAxios, getConAxios, editConAxios
 } from '../../../../../api/dataSource/tableManage';

import { message } from 'antd';

const getDataSource = (dataSource) => ({
  type: constants.GET_DATASOURCE,
  dataSource: fromJS(dataSource),
});

export const getDataSourceOperation = (userName) => {
  return(dispatch) => {
    getAllConfigsAxios(userName).then((res) => {
      if(res){
        if(res.data.ok){
          const dataSource = res.data.data;
          console.log(dataSource);
          dispatch(getDataSource(dataSource));
        }
      }
    });
  }
};

export const searchDataSourceOperation = (userName,searchValue) => {
  return(dispatch) => {
    getAllConfigsAxios(userName).then((res) => {
      if(res){
        if(res.data.ok){
          const dataSource = res.data.data;
          let filterData = dataSource.filter(item => item.configName.includes(searchValue));
          dispatch(getDataSource(filterData));
        }
      }
    });
  }
};

const getAllDatabase = (database) => ({
  type: constants.GET_ALL_DATABASE,
  database: fromJS(database),
});

export const getAllDatabaseOperation = (dataType,params) => {
  return(dispatch) => {
    getAllDatabaseAxios(dataType,params).then((res)=>{
      if(res){
        if(res.data.ok){
          const database = res.data.data;
          dispatch(getAllDatabase(database));
        }
      }
    });
  }
}

const cleanDatabase = () => ({
  type: constants.GET_ALL_DATABASE,
  database: [],
});

export const cleanDatabaseOperation = () => {
  return(dispatch)=>{
    dispatch(cleanDatabase());
  }
}

export const testConOperation = (dataType,params) => {
  return(dispatch)=>{
    testConAxios(dataType,params).then((res)=>{
      if(res){
        if(res.data.ok){
          message.success("测试成功！")
          let dt = dataType.toLowerCase();
          if(dt==="mysql"||dt==="sqlserver"||dt==="mongodb"){
            dispatch(getAllDatabaseOperation(dt,params));
          }
        }
        else{
          message.error(res.data.msg);
          let dt = dataType.toLowerCase();
          if(dt==="mysql"||dt==="sqlserver"||dt==="mongodb"){
            dispatch(getAllDatabase([]));
          }
        }
      }
    });
  }
}

export const saveConOperation = (dataType,params,searchValue) => {
  return(dispatch)=>{
    saveConAxios(dataType,params).then((res)=>{
      if(res){
        if(res.data.ok){
          message.success("添加成功！");
          if(searchValue===""){
            dispatch(getDataSourceOperation(params.username));
          }
          else{
            dispatch(searchDataSourceOperation(params.username,searchValue));
          }
        }
        else{
          message.error("添加失败！",res.data.msg)
        }
      }
    });
  }
}

export const deleteConOperation = (dataType,params) => {
  return(dispatch)=>{
    deleteConAxios(dataType,params).then((res)=>{
      if(res){
        if(res.data.ok){
          message.success("删除成功！")
          dispatch(getDataSourceOperation());
        }
        else{
          message.error("删除失败！",res.data.msg)
        }
      }
    });
  }
}

export const editConOperation = (dataType,params,searchValue) => {
  return(dispatch)=>{
    editConAxios(dataType,params).then((res)=>{
      if(res){
        if(res.data.ok){
          message.success("编辑成功！");
          if(searchValue===""){
            dispatch(getDataSourceOperation(params.username));
          }
          else{
            dispatch(searchDataSourceOperation(params.username,searchValue));
          }
        }
        else{
          message.error("编辑失败！",res.data.msg);
        }
      }
    });
  }
}

const getCon = (databaseConfig,basicInfo,database) => ({
  type: constants.GET_CON,
  //conInfo: fromJS(conInfo),
  databaseConfig: fromJS(databaseConfig),
  basicInfo: fromJS(basicInfo),
  database: fromJS(database),
});

export const getConOperation = (dataType,params) => {
  return(dispatch)=>{
    getConAxios(dataType,params).then((res)=>{
      if(res){
        if(res.data.ok){
          const conInfo = res.data.data;
          let databaseConfig = JSON.parse(conInfo.databaseConfig);
          let basicInfo = JSON.parse(conInfo.basicInfo);
          let dataType = databaseConfig.databaseType.toLowerCase();
          if(dataType==="mysql"||dataType==="sqlserver"||dataType==="mongodb"){
              let host = databaseConfig.hostName;
              let port = databaseConfig.port;
              let userName = databaseConfig.userName;
              let password = databaseConfig.password;
              let dbparams = {host,port,userName,password}
              getAllDatabaseAxios(dataType,dbparams).then((res)=>{
                if(res){
                  if(res.data.ok){
                    const database = res.data.data;
                    dispatch(getCon(databaseConfig,basicInfo,database));
                  }
                }
              });
          }else{
            dispatch(getCon(databaseConfig,basicInfo,[]));
          }
        }
      }
    });
  }
}

export const clearConfigOperation = () => {
  return(dispatch)=>{
    dispatch(getCon({},{},[]));
  }
}